package com.jt.www.common.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jt.www.common.service.SchoolService;
import com.jt.www.dao.mapper.school.SchoolInfoMapper;
import com.jt.www.enums.ResultEnum;
import com.jt.www.model.reps.ResultDTO;
import com.jt.www.model.school.School;
import com.jt.www.model.school.vo.SchoolVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

@Service
public class SchoolServiceImpl implements SchoolService {

    @Autowired
    private SchoolInfoMapper schoolInfoMapper;

    @Override
    public Integer insertSchoolInfo(School school) {
        //添加学校必须有学校名称
        if (school == null || StringUtils.isBlank(school.getSchoolName())) {
            return -1;//参数传入不正确,学校名不能为空
        }

        if (StringUtils.isBlank(school.getProvinceCode())
                || StringUtils.isBlank(school.getCityCode())
                || StringUtils.isBlank(school.getCountyCode())) {
            return -1;//参数传入不正确,学校名、省、市、区编码不能为空
        }

        //按学校名精确查找学校信息
        List<School> schools = schoolInfoMapper.querySchoolByNameAndArea(school);

        if (schools.size() > 0) {
            if (schools.get(0).getIsDeleted()) {
                schools.get(0).setIsDeleted(false);
                schoolInfoMapper.updateByPrimaryKeySelective(schools.get(0));
                return 1;//添加成功
            } else {
                return -2;//学校名已存在不能重复添加
            }
        }


        String uuid = UUID.randomUUID().toString();
        String keyUUid = uuid.replaceAll("-", "");
        school.setSchoolCode(keyUUid);
        school.setIsDeleted(false);
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        school.setCreationTime(dateFormat.format(date));
        int num = schoolInfoMapper.insert(school);

        return num;
    }


    @Override
    public int deleteSchoolById(Integer id) {
        School school = new School();
        school.setId(Long.valueOf(id));
        school.setIsDeleted(true);

        int num = schoolInfoMapper.updateByPrimaryKeySelective(school);

        return num;
    }

    @Override
    public Integer updateSchoolInfo(School school) {
        if (school == null) {
            return -1;
        }
        int num = schoolInfoMapper.updateByPrimaryKeySelective(school);

        return num;
    }

    @Override
    public ResultDTO searchSchoolByNameAddressPhone(SchoolVo school) {
        if (school.getPage() != null && school.getRows() != null) {
            PageHelper.startPage(school.getPage(), school.getRows());
        }
        List<School> resultList = schoolInfoMapper.querySchool(school);
        //处理时间显示
        for (School sc : resultList) {
            sc.setCreationTime(sc.getCreationTime().substring(0, 19));
        }
        PageInfo pageinfo = new PageInfo(resultList);
        HashMap<String, Object> map = new HashMap<>();
        map.put("result", resultList);
        map.put("total", pageinfo.getTotal());

        return new ResultDTO(ResultEnum.SUCCESS_NO_MSG, map);
    }

}
